package com.example.note.repository;

import android.app.Application;
import androidx.lifecycle.LiveData;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.example.note.database.DiaryDao;
import com.example.note.database.DiaryEntity;
import com.example.note.database.NoteDatabase;

public class DiaryRepository {
    private DiaryDao diaryDao;
    private LiveData<List<DiaryEntity>> allDiaries;
    private ExecutorService executor;
    
    public DiaryRepository(Application application) {
        NoteDatabase database = NoteDatabase.getInstance(application);
        diaryDao = database.diaryDao();
        allDiaries = diaryDao.getAllDiaries();
        executor = Executors.newFixedThreadPool(2);
    }
    
    public LiveData<List<DiaryEntity>> getAllDiaries() {
        return allDiaries;
    }
    
    public LiveData<DiaryEntity> getDiaryById(int id) {
        return diaryDao.getDiaryById(id);
    }
    
    public LiveData<List<DiaryEntity>> getDiariesByDate(String date) {
        return diaryDao.getDiariesByDate(date);
    }
    
    public LiveData<List<DiaryEntity>> searchDiaries(String keyword) {
        return diaryDao.searchDiaries(keyword);
    }
    
    public LiveData<List<DiaryEntity>> getHandwrittenDiaries() {
        return diaryDao.getHandwrittenDiaries();
    }
    
    public LiveData<Integer> getDiaryCount() {
        return diaryDao.getDiaryCount();
    }
    
    public void insertDiary(DiaryEntity diary) {
        executor.execute(() -> diaryDao.insertDiary(diary));
    }
    
    public void updateDiary(DiaryEntity diary) {
        executor.execute(() -> diaryDao.updateDiary(diary));
    }
    
    public void deleteDiary(DiaryEntity diary) {
        executor.execute(() -> diaryDao.deleteDiary(diary));
    }
    
    public void deleteDiaryById(int id) {
        executor.execute(() -> diaryDao.deleteDiaryById(id));
    }
}